package com.etl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.etl.constants.UserSession;
import com.etl.enums.ResultEnum;
import com.etl.exception.GlobalException;
import com.etl.service.IEtlBusinessBatchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.etl.entity.EtlBusinessBatch;
import com.etl.entity.EtlBusinessDbInfo;
import com.etl.mapper.EtlBusinessBatchMapper;
import com.etl.mapper.EtlBusinessDbInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ：poplar
 * @date ：Created in 2020/4/24 5:39 下午
 * @description：批次
 * @modified By：
 * @version: 1.0$
 */
@Service
public class EtlBusinessBusinessBatchServiceImpl implements IEtlBusinessBatchService {

    @Resource
    private EtlBusinessBatchMapper etlBusinessBatchMapper;

    @Resource
    private EtlBusinessDbInfoMapper etlBusinessDbInfoMapper;


    @Override
    public PageInfo<EtlBusinessBatch> getEtlBatchList(Integer pages, Integer sizes,String batchName) {
        PageHelper.startPage(pages, sizes);
        QueryWrapper<EtlBusinessBatch> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(batchName)){
            queryWrapper.like("batchName",batchName);
        }


        //普通用户的话,只能查询自己的任务
        if(UserSession.getUser().getUserType() == 0){
            queryWrapper.eq("create_account",UserSession.getUser().getAccountName());
        }



        queryWrapper.orderByDesc("update_time");
        List<EtlBusinessBatch> etlBusinessBatches = etlBusinessBatchMapper.selectList(queryWrapper);
        PageInfo<EtlBusinessBatch> pageInfoDemo = new PageInfo(etlBusinessBatches);
        return pageInfoDemo;
    }

    @Override
    public void addBatch(String batchName,Integer sourceDbId,Integer targetDbId) {
        /**
         * 1.校验系统名称唯一性
         */
        QueryWrapper<EtlBusinessBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_name", batchName);
        queryWrapper.eq("create_account",UserSession.getUser().getAccountName());
        List<EtlBusinessBatch> etlBusinessBatches = etlBusinessBatchMapper.selectList(queryWrapper);
        if (etlBusinessBatches.size() > 0) {
            throw new GlobalException(ResultEnum.BATCH_SYSTEM_NAME_EXIST);
        }

        EtlBusinessDbInfo sourceEtlBusinessDbInfo = etlBusinessDbInfoMapper.selectById(sourceDbId);
        if ( sourceEtlBusinessDbInfo == null) {
            throw new GlobalException(ResultEnum.DB_SYSTEM_NAME_NOT_EXIST);
        }

        EtlBusinessDbInfo targetEtlBusinessDbInfo = etlBusinessDbInfoMapper.selectById(targetDbId);
        if ( targetEtlBusinessDbInfo == null) {
            throw new GlobalException(ResultEnum.DB_SYSTEM_NAME_NOT_EXIST);
        }


        /**
         * 3.持久化
         */
        EtlBusinessBatch insertBatch = new EtlBusinessBatch();
        insertBatch.setBatchName(batchName)
                  .setSourceDbId(sourceDbId)
                  .setSourceDbSysname(sourceEtlBusinessDbInfo.getDbSysname())
                 .setTargetDbId(targetEtlBusinessDbInfo.getDbId())
                 .setTargetDbSysname(targetEtlBusinessDbInfo.getDbSysname())
                 .setExecFlag(0)
                 .setFlag(0)
                 .setCreateAccount(UserSession.getUser().getAccountName())
                 .setUpdateAccount(UserSession.getUser().getAccountName())
                 .setCreateTime(new Date());
        etlBusinessBatchMapper.insert(insertBatch);
    }


    /**
     * 【接口】更新批次
     *
     * @param businessBatch 批次实体类
     */
    @Override
    public void updateBatch(EtlBusinessBatch businessBatch) {

        EtlBusinessDbInfo sourceEtlBusinessDbInfo = etlBusinessDbInfoMapper.selectById(businessBatch.getSourceDbId());
        if ( sourceEtlBusinessDbInfo == null) {
            throw new GlobalException(ResultEnum.DB_SYSTEM_NAME_NOT_EXIST);
        }

        EtlBusinessDbInfo targetEtlBusinessDbInfo = etlBusinessDbInfoMapper.selectById(businessBatch.getTargetDbId());
        if ( targetEtlBusinessDbInfo == null) {
            throw new GlobalException(ResultEnum.DB_SYSTEM_NAME_NOT_EXIST);
        }
        businessBatch.setSourceDbId(sourceEtlBusinessDbInfo.getDbId())
                .setSourceDbSysname(sourceEtlBusinessDbInfo.getDbSysname())
                .setTargetDbId(targetEtlBusinessDbInfo.getDbId())
                .setTargetDbSysname(targetEtlBusinessDbInfo.getDbSysname());


        businessBatch.setUpdateAccount(UserSession.getUser().getAccountName());
        businessBatch.setUpdateTime(new Date());


//        QueryWrapper<EtlBusinessBatch> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("batch", businessBatch.getBatch());
        etlBusinessBatchMapper.updateById(businessBatch);
    }




    /**
     * 【接口】更新批次
     *
     * @param businessBatch 批次实体类
     */
    @Override
    public void startBatch(EtlBusinessBatch businessBatch) {

        businessBatch.setUpdateAccount(UserSession.getUser().getAccountName());
        businessBatch.setUpdateTime(new Date());

//        QueryWrapper<EtlBusinessBatch> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("batch", businessBatch.getBatch());
        etlBusinessBatchMapper.updateById(businessBatch);
    }






}
